package com.example.myapplication.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.myapplication.R;
import com.example.myapplication.ui.ProgressWheelView;
import com.example.myapplication.ui.PullToRefreshView;
import com.example.myapplication.api.Api;
import com.example.myapplication.api.TtitCallback;
import com.example.myapplication.ui.SuperProgressWheel;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

public class ListViewFragment extends BaseRefreshFragment {

    private TextView ui_temperature_text;
    private TextView ui_cityinfo_text;
    private TextView ui_weather_text;
    private TextView temperature_text;
    private TextView humidity_text;

    private ImageView ui_weather_icon;

    private SuperProgressWheel temp_ProgressWheel;
    private SuperProgressWheel humi_ProgressWheel;

    private PullToRefreshView mPullToRefreshView;

    private static boolean http_get_falg = false;
    private Thread mThread;
    private boolean running = true;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list_view, container, false);

        ListView listView = rootView.findViewById(R.id.list_view);
        listView.setAdapter(new SampleAdapter(getActivity(), R.layout.list_item, mSampleList));

        mPullToRefreshView = rootView.findViewById(R.id.pull_to_refresh);
        mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPullToRefreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPullToRefreshView.setRefreshing(false);
                        // 获取天气
                        boolean flag = get_weather_http_api();
                        if (flag) {
                            Toast.makeText(getActivity(), "success", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getActivity(), "fail", Toast.LENGTH_SHORT).show();
                        }
                    }
                }, REFRESH_DELAY);
            }
        });

        return rootView;
    }

    class SampleAdapter extends ArrayAdapter<Map<String, Integer>> {

        public static final String KEY_COLOR = "color";

        private final LayoutInflater mInflater;
        private final List<Map<String, Integer>> mData;

        public SampleAdapter(Context context, int layoutResourceId, List<Map<String, Integer>> data) {
            super(context, layoutResourceId, data);
            mData = data;
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
            final ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.list_item, parent, false);
                convertView.setTag(viewHolder);

                ui_temperature_text = convertView.findViewById(R.id.temperature);
                ui_cityinfo_text = convertView.findViewById(R.id.city);
                ui_weather_text = convertView.findViewById(R.id.weather);
                ui_weather_icon = convertView.findViewById(R.id.weather_img);
                temperature_text = convertView.findViewById(R.id.temp_text);
                humidity_text = convertView.findViewById(R.id.humi_text);
                //
                temp_ProgressWheel = convertView.findViewById(R.id.spw1);
                humi_ProgressWheel = convertView.findViewById(R.id.spw2);

                temperature_text.setText("25");
                humidity_text.setText("53");
                //
                ui_temperature_text.getPaint().setFakeBoldText(true);
                ui_cityinfo_text.getPaint().setFakeBoldText(true);
                ui_weather_text.getPaint().setFakeBoldText(true);
//                temp_ProgressWheel.setOnProgressListener();
//                humi_ProgressWheel.setOnProgressListener();
                temp_ProgressWheel.setProgress(20);
                humi_ProgressWheel.setProgress(30);
                Test_thread();
                // 获取天气
                get_weather_http_api();
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            convertView.setBackgroundResource(mData.get(position).get(KEY_COLOR));
            return convertView;
        }

        class ViewHolder {
            ImageView imageViewIcon;
        }
    }

    private void Test_thread() {
        mThread = new Thread(new Runnable() {
            @SuppressLint("SetTextI18n")
            @Override
            public void run() {
                while (running) {
                    temp_ProgressWheel.setProgress(temp_ProgressWheel.getProgress() + 1);
                    humi_ProgressWheel.setProgress(humi_ProgressWheel.getProgress() + 1);

                    if (temp_ProgressWheel.getProgress() + 1 == 80) {
                        temp_ProgressWheel.setProgress(0);
                    }
                    if (humi_ProgressWheel.getProgress() + 1 == 100) {
                        humi_ProgressWheel.setProgress(0);
                    }
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        mThread.start();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (running) {
            System.out.println("Timer onPose");
            running = false;
            mThread.interrupt();
        }
    }

    private boolean get_weather_http_api() {
        Api.config("").getRequest(getActivity(), new TtitCallback() {
            @Override
            public void onSuccess(String res) {
                getActivity().runOnUiThread(new Runnable() {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void run() {
                        try {
                            JSONObject jsonObject = new JSONObject(res);
                            // temperature
                            String http_temperature = jsonObject.getJSONArray("results").getJSONObject(0).getJSONObject("now").getString("temperature");
                            ui_temperature_text.setText(http_temperature + "℃");
                            // city
                            String http_city_name = jsonObject.getJSONArray("results").getJSONObject(0).getJSONObject("location").getString("name");
                            ui_cityinfo_text.setText(http_city_name);
                            // weather_info
                            String http_weather_text = jsonObject.getJSONArray("results").getJSONObject(0).getJSONObject("now").getString("text");
                            ui_weather_text.setText(http_weather_text);
                            // code
                            int weather_code = Integer.parseInt(jsonObject.getJSONArray("results").getJSONObject(0).getJSONObject("now").getString("code"));
                            setWeatherImg(weather_code);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                http_get_falg = true;
            }

            @Override
            public void onFailure(Exception e) {
                Toast.makeText(getActivity(), "网络连接错误", Toast.LENGTH_SHORT).show();
                System.out.println("ConnectFragment----------网络连接错误------------" + e.toString());
                http_get_falg = false;
            }
        });
        return http_get_falg;
    }

    @SuppressLint("ResourceType")
    private void setWeatherImg(int weather_code) {
        switch (weather_code) {
            case 0:
                ui_weather_icon.setImageResource(R.drawable.icon_1);
                break;
            case 1:
                ui_weather_icon.setImageResource(R.drawable.icon_2);
                break;
            case 2:
                ui_weather_icon.setImageResource(R.drawable.icon_3);
                break;
            case 3:
                ui_weather_icon.setImageResource(R.drawable.icon_4);
                break;
            case 4:
                ui_weather_icon.setImageResource(R.drawable.icon_5);
                break;
            case 5:
                ui_weather_icon.setImageResource(R.drawable.icon_6);
                break;
            case 6:
                ui_weather_icon.setImageResource(R.drawable.icon_7);
                break;
            case 7:
                ui_weather_icon.setImageResource(R.drawable.icon_8);
                break;
            case 8:
                ui_weather_icon.setImageResource(R.drawable.icon_9);
                break;
            case 9:
                ui_weather_icon.setImageResource(R.drawable.icon_10);
                break;
            case 10:
                ui_weather_icon.setImageResource(R.drawable.icon_11);
                break;
            case 11:
                ui_weather_icon.setImageResource(R.drawable.icon_12);
                break;
            case 12:
                ui_weather_icon.setImageResource(R.drawable.icon_13);
                break;
            case 13:
                ui_weather_icon.setImageResource(R.drawable.icon_14);
                break;
            case 14:
                ui_weather_icon.setImageResource(R.drawable.icon_15);
                break;
            case 15:
                ui_weather_icon.setImageResource(R.drawable.icon_16);
                break;
            case 16:
                ui_weather_icon.setImageResource(R.drawable.icon_17);
                break;
            case 17:
                ui_weather_icon.setImageResource(R.drawable.icon_18);
                break;
            case 18:
                ui_weather_icon.setImageResource(R.drawable.icon_19);
                break;
            case 19:
                ui_weather_icon.setImageResource(R.drawable.icon_20);
                break;
            case 20:
                ui_weather_icon.setImageResource(R.drawable.icon_21);
                break;
            case 21:
                ui_weather_icon.setImageResource(R.drawable.icon_22);
                break;
            case 22:
                ui_weather_icon.setImageResource(R.drawable.icon_23);
                break;
            case 23:
                ui_weather_icon.setImageResource(R.drawable.icon_24);
                break;
            case 24:
                ui_weather_icon.setImageResource(R.drawable.icon_25);
                break;
            case 25:
                ui_weather_icon.setImageResource(R.drawable.icon_26);
                break;
            case 26:
                ui_weather_icon.setImageResource(R.drawable.icon_27);
                break;
            case 27:
                ui_weather_icon.setImageResource(R.drawable.icon_28);
                break;
            case 28:
                ui_weather_icon.setImageResource(R.drawable.icon_29);
                break;
            case 29:
                ui_weather_icon.setImageResource(R.drawable.icon_30);
                break;
            case 30:
                ui_weather_icon.setImageResource(R.drawable.icon_31);
                break;
            case 31:
                ui_weather_icon.setImageResource(R.drawable.icon_32);
                break;
            case 32:
                ui_weather_icon.setImageResource(R.drawable.icon_33);
                break;
            case 33:
                ui_weather_icon.setImageResource(R.drawable.icon_34);
                break;
            case 34:
                ui_weather_icon.setImageResource(R.drawable.icon_35);
                break;
            case 35:
                ui_weather_icon.setImageResource(R.drawable.icon_36);
                break;
            case 36:
                ui_weather_icon.setImageResource(R.drawable.icon_37);
                break;
            case 37:
                ui_weather_icon.setImageResource(R.drawable.icon_38);
                break;
            case 38:
                ui_weather_icon.setImageResource(R.drawable.icon_39);
                break;
            case 99:
                ui_weather_icon.setImageResource(R.drawable.icon_40);
                break;
        }
    }
}
